package org.usmile.algorithms.huawei.acm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

// https://blog.csdn.net/qq_34465338/article/details/128921504
public class _统一限载货物数最小值 {
    public static int k;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();

        int[] goods = new int[len];
        for (int i = 0; i < len; i++) {
            goods[i] = sc.nextInt();
        }

        int[] types = new int[len];
        for (int i = 0; i < len; i++) {
            types[i] = sc.nextInt();
        }

        k = sc.nextInt();

        List<Integer> dryList = new ArrayList<>();      //干货物
        List<Integer> wetList = new ArrayList<>();      //湿货物
        int dryWeight = 0;       //干货物总重
        int wetWeight = 0;       //湿货物总重
        for (int i = 0; i < len; i++) {
            int type = types[i];
            int good = goods[i];
            if (type == 0) {
                dryList.add(good);
                dryWeight += good;
            } else {
                wetList.add(good);
                wetWeight += good;
            }
        }

        Collections.sort(dryList);
        Collections.sort(wetList);

        int dryMin = 0;
        int wetMin = 0;
        if (dryList.size() != 0) {
            dryMin = getMin(dryList, dryWeight);
        }
        if (wetList.size() != 0) {
            wetMin = getMin(wetList, wetWeight);
        }
        int res = Math.max(dryMin, wetMin);

        System.out.println(res);
    }

    public static int getMin(List<Integer> goodList, int totalWeight) {
        //最重货物
        int maxList = goodList.get(goodList.size() - 1);
        //平均每辆车放置的最低重量
        int minWeight = totalWeight % k == 0 ? totalWeight / k : totalWeight / k + 1;
        //最少限载货物量
        int min = Math.max(maxList, minWeight);
        //最大限载货物量（一辆车的时候）
        int max = totalWeight;

        //二分法
        while (min < max) {
            int mid = (min + max) / 2;
            //K辆车（每次都需要初始化）
            int[] vans = new int[k];
            if (check(goodList, 0, vans, mid)) {
                max = mid;
            } else {
                min = mid + 1;
            }
        }

        return min;
    }

    public static boolean check(List<Integer> goods, int index, int[] vans, int perCapacity) {
        if (index == goods.size()) {
            return true;
        }
        for (int i = 0; i < vans.length; i++) {
            //多辆车货物重量一样,前面已经试了不要重复试,此处可能不好理解
            if (i > 0 && vans[i] == vans[i - 1]) {
                continue;
            }
            // 满足条件才能放入
            if (vans[i] + goods.get(index) <= perCapacity) {
                // 放入
                vans[i] = vans[i] + goods.get(index);
                // 后续递归放入剩余的货物
                if (check(goods, index + 1, vans, perCapacity)) {
                    return true;
                }
                //上面的策略失败了,就回退,继续尝试后面的策略
                vans[i] = vans[i] - goods.get(index);
            }
        }
        return false;
    }
}
